<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>set数据结构</title>
  </head>
  <body>
    <div></div>
    <script>
      //1、对数组快速去重
      let arr =[1,2,3,2,1,4,5,6];
      let result = [...new Set(arr)];
      console.log(result)//无需循环遍历

    
      let a = [1,2,3];
      let b = [4,3,2];
      //2、获取两个元素的并集
      let union = [...new Set([...a,...b])]
      console.log(union);//[1,2,3,4]

      //3、获取交集
      let result3 = [...new Set(a)].filter((item)=>{
        return new Set(b).has(item)
      })
      //简写
      //let result4 = [...new Set(a)].filter(item=> new Set(b).has(item));
      console.log(result3)

      //4、获取a相对于b的差集
      let diff = [...new Set(a)].filter(item => !new Set(b).has(item))
      console.log(diff)

      // for (const v of result) {
      //   console.log(v)
      // }
    </script>
  </body>
</html>
